package com.webserver.core;

import com.webserver.annotation.Controller;
import com.webserver.annotation.RequestMapping;
import com.webserver.controller.UserController;
import com.webserver.http.HttpServeletRequest;
import com.webserver.http.HttpServeletResponse;

import java.io.File;
import java.io.IOException;
import java.lang.annotation.Annotation;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.net.URISyntaxException;

public class DispatcherServlet {
    //单例
    private static DispatcherServlet instance = new DispatcherServlet();
    private DispatcherServlet() {}
    public static DispatcherServlet getInstance(){ return instance; }

    //找到类加载路径的static目录
    private static File root;
    private static File staticDir;
    static {
        try {
            root = new File(
              DispatcherServlet.class.getClassLoader().getResource(".").toURI()
            );
            staticDir = new File(root,"static");
        } catch (URISyntaxException e) {
            e.printStackTrace();
        }
    }
   public void service(HttpServeletRequest request,HttpServeletResponse response) throws IOException, ClassNotFoundException {
        String path = request.getRequestURI();
       try {
           File dir = new File(DispatcherServlet.class.getClassLoader().getResource("./com/webserver/controller").toURI());
           File[] files = dir.listFiles(f->f.getName().endsWith(".class"));
           for (File file : files){
               String flieName = file.getName();
               String className = flieName.substring(0,flieName.indexOf("."));
               String packageName = dir.getName();
               Class cls = Class.forName("com.webserver.controller."+className);
               if (cls.isAnnotationPresent(Controller.class)){
                   Object obj = cls.newInstance();
                   Method[] methods = cls.getDeclaredMethods();
                   for (Method m : methods){
                       if (m.isAnnotationPresent(RequestMapping.class)){
                           RequestMapping rm = m.getAnnotation(RequestMapping.class);
                           String para = rm.value();
                           if (para.equals(path)){
                               m.invoke(obj,request,response);
                               return;
                           }
                       }
                   }

               }
           }

       } catch (URISyntaxException e) {
           e.printStackTrace();
       } catch (InstantiationException e) {
           e.printStackTrace();
       } catch (IllegalAccessException e) {
           e.printStackTrace();
       } catch (InvocationTargetException e) {
           e.printStackTrace();
       }

       File file = new File(staticDir,path);
       if (file.isFile()){
           response.setContentFile(file);
       }else {
           response.setStatusCode(404);
           response.setStatusReason("NotFound");
           file = new File(staticDir,"/404.html");
           response.setContentFile(file);
        }
       response.addHeasers("Server","BirdServer");


   }



}
